var index={
    config:{datatype:[]},
    Show:function(){
        m.request({
            method:"GET",
            url:"config.json",
        }).then(function(res){
            index.config=res;
            document.querySelector("title").innerText=index.config.title;
            m.render(document.body,m(index))
        })
    },
    view:function(){
        return [
            m("div.top",index.config.title),
            m("div.map",[
                m("button.map_bt_active",{onclick:index.Change},"数据字典"),
                m("button.map_bt",{onclick:index.Change},"表结构"),
                m("button.map_bt",{onclick:index.Change},"接口定义")
            ]),
            m("div.page"),
            m("datalist#datatype",index.config.datatype.map(function(val){
                return m("option",{value:val})
            }))
        ]
    },
    oncreate:function(){
        document.querySelector(".map_bt_active").click();
    },
    Change:function(){
        document.querySelectorAll(".map_bt,.map_bt_active").forEach((bt)=>{
            // console.log(bt.getAttribute("class"));
            bt.setAttribute("class","map_bt");
        });
        this.setAttribute("class","map_bt_active");
        if(this.innerText=="数据字典"){
            dictionary.Show();
            return;
        }
        if(this.innerText=="表结构"){
            database.Show();
            return;
        }
        if(this.innerText=="接口定义"){
            interface.Show();
            return;
        }
    }
}


//数据指定处理
var dictionary={
    list:[],
    Show:function(){
        m.request({
            method:"GET",
            url:"query.php?dictionary=1"
        }).then(function(res){
            dictionary.list=[];
            if(res!={}){
                count=1;
                for(var key in res){
                    dictionary.list.push({
                        count:count,
                        desc:res[key].desc,
                        key:key,
                        content:res[key].content,
                        type:res[key].type,
                        min:res[key].min,
                        max:res[key].max,
                        regex:res[key].regex
                    });
                    count++;
                }
                console.log(dictionary.list);
            }
            m.render(document.querySelector(".page"),m(dictionary));
        })
    },
    view:function(){
        return [
            m("table.tbl",[
                m("tr",[
                    m("th","编号"),
                    m("th","中文名称"),
                    m("th","英文名称"),
                    m("th","规则简述"),
                    m("th","类别"),
                    m("th","最小字符"),
                    m("th","最大字符"),
                    m("th","正则表达式")
                ]),
                dictionary.list.map(function(tr){
                    return m("tr",[
                        m("td",tr.count),
                        m("td",tr.desc),
                        m("td",tr.key),
                        m("td",tr.content),
                        m("td",tr.type),
                        m("td",tr.min),
                        m("td",tr.max),
                        m("td",tr.regex)
                    ])
                })
            ])
        ]
    }
}

//数据库处理
var database={
    list:[],
    view:function(){
        return [
            m("div.boxes",database.ShowTable()),
            m("datalist#dict",dictionary.list.map((item)=>{
                return m("option",{value:item.key},item.desc);
            }))
        ]
    },
    ShowTable:function(){
        return database.list.map(function(box){
            var jsonToArray=[];
            for(var key in box.column){
                jsonToArray.push({
                    key:key,
                    desc:box.column[key].desc,
                    type:box.column[key].type,
                    isNull:box.column[key].isNull,
                    index:box.column[key].index,
                });
            }
            return m("div.box",[
                m("div.tbl_name",[
                    m("input[type=text]",{placeholder:"表名称",value:box.table,readonly:true})
                ]),
                m("table.tbl",[
                    m("tr",[
                        m("th","栏位"),
                        m("th","栏位名称"),
                        m("th","类型"),
                        m("th","是否可空"),
                        m("th","索引")
                    ]),
                    jsonToArray.map(function(item){
                        return m("tr",[
                            m("td",item.key),
                            m("td",item.desc),
                            m("td",item.type),
                            m("td",item.isNull),
                            m("td",item.index)
                        ])
                    })
                ])
            ])
        })
    },
    Show:function(){
        // m.render(document.querySelector(".page"),m(database));
        database.list=[];
        m.request({
            method:"GET",
            url:"query.php?database=1"
        }).then(function(res){
            console.log(res);
            if(res.length>0){
                database.list=res;
                m.render(document.querySelector(".page"),m(database));
            }
        })
    }
}

//接口结构
var interface={
    list:[],
    view:function(){
        return [
            m("div.urls",[
                interface.ShowURL()
            ]),
            m("datalist#dict",dictionary.list.map((item)=>{
                return m("option",{value:item.key},item.desc);
            }))
        ]
    },
    Show:function(){
        interface.list=[];
        m.request({
            method:"GET",
            url:"query.php?interface=1"
        }).then(function(res){
            interface.list=res;
            m.render(document.querySelector(".page"),m(interface));
        })
    },
    ChangeBox:function(){
        this.parentNode.parentNode.querySelectorAll(".box_content").forEach((elem)=>{
            elem.style.height=this.innerText=="收起"?"0px":"auto";
            elem.style.transition="all 0.3s";
        });
        
        this.innerText=this.innerText=="收起"?"展开":"收起";
    },
    ShowURL:function(){
        return interface.list.map(function(item){
            var request=[];
            var response=[];
            for(var key in item.request){
                request.push({key:key,desc:item.request[key].desc,type:item.request[key].type,usecase:item.request[key].usecase,regex:item.request[key].regex,isMust:item.request[key].isMust});
            }
            for(var key in item.response){
                response.push({key:key,desc:item.response[key].desc,type:item.response[key].type,usecase:item.response[key].usecase,regex:item.response[key].regex,isMust:item.response[key].isMust});
            }
            console.log(request);
            return m("div.url",[
                m("div.box_title",[
                    m("input[type=text].box_name",{placeholder:"接口名称",value:item.desc}),
                    m("select.box_method",[
                        m("option",{value:"GET",selected:item.method=="GET"?true:false},"GET index.php?p="),
                        m("option",{value:"POST",selected:item.method=="POST"?true:false},"POST index.php?p=")
                    ]),
                    m("input[type=text].box_input",{value:item.url}),
                    m("button",{onclick:interface.ChangeBox},"展开")
                ]),
                m("div.box_content",{style:"height:0px;"},[
                    m("div.box_tag","Request"),
                    m("div.box_line",[
                        m("div.box_sub","Body")
                    ]),
                    m("table.tbl2",[
                        m("tr",[
                            m("th","参数"),
                            m("th","说明"),
                            m("th","类型"),
                            m("th","举例"),
                            m("th","约束"),
                            m("th","是否必须")
                        ]),
                        request.map(function(_r){
                            return m("tr",[
                                m("td",_r.key),
                                m("td",_r.desc),
                                m("td",_r.type),
                                m("td",_r.usecase),
                                m("td",_r.regex),
                                m("td",_r.isMust)
                            ])
                        })
                    ]),
                ]),
                m("div.box_content",{style:"height:0px;"},[
                    m("div.box_tag","Response"),
                    m("div.box_line",[
                        m("div.box_sub","JSON")
                    ]),
                    m("table.tbl2",[
                        m("tr",[
                            m("th","参数"),
                            m("th","说明"),
                            m("th","类型"),
                            m("th","举例"),
                            m("th","约束"),
                            m("th","是否必须")
                        ]),
                        response.map(function(_r){
                            return m("tr",[
                                m("td",_r.key),
                                m("td",_r.desc),
                                m("td",_r.type),
                                m("td",_r.usecase),
                                m("td",_r.regex),
                                m("td",_r.isMust)
                            ])
                        })
                    ])
                ])
            ])
        })
    }
}


//入口函数
index.Show();